﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace NCKH
{
    public partial class Home : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Name"] != null && Session["Name"] != "")
            {
                lblThongbao.Text = "Xin chào " + Session["Name"].ToString() + ",";
                plDangnhap.Visible = false;
                lbtnThoat.Visible = true;
                string Name = Session["Name"].ToString();
                int quyen = layMaquyen(Name);
                if (quyen == 1)
                {
                    lbtnQuantri.Visible = true;
                }
                if (quyen == 2)
                {
                    lbtnThongtinNNC.Visible = true;
                    lbtnThongtinSV.Visible = true;
                    lbtnCanhan.Visible = true;
                }
                if (quyen == 3)
                    lbtnCanhan.Visible = true;
            }
        }
        protected int layMaquyen(string Name)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string selectQuyen = "SELECT MaQ FROM TAIKHOAN where Tendangnhap='" + Name + "'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(selectQuyen, Cnn))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        DataRow dr = tbl.Rows[0];
                        object mq = dr["MaQ"];
                        int quyen = (int)mq;
                        return quyen;
                    }
                }
            }
        }
        protected void btndangnhap_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string selectUser = "SELECT * FROM TAIKHOAN where Tendangnhap='" + txtuser.Text + "'and Matkhau='" + txtpassword.Text + "'";
            SqlConnection cnn = new SqlConnection(connectionString);
            SqlDataAdapter da = new SqlDataAdapter(selectUser, cnn);
            DataTable tbl = new DataTable();
            da.Fill(tbl);
            if (tbl.Rows.Count > 0)
            {
                Session["Name"] = txtuser.Text;
                Session["Pass"] = txtpassword.Text;
                //Session["alow"] = true;
                plDangnhap.Visible = false;
                DataRow dr = tbl.Rows[0];
                object maq = dr["MaQ"];
                int quyen = (int)maq;
                if (quyen == 1)
                {
                    Response.Write("Đăng nhập thành công");
                    Response.Redirect("Trangchu.aspx");
                    

                }
                if (quyen == 2)
                {
                    Response.Write("Đăng nhập thành công");
                    Response.Redirect("Trangchu.aspx");
                    //Response.Redirect(Request.Url + "");
                }
                if (quyen == 3)
                {
                    Response.Redirect("Trangchu.aspx");
                }
                boqua();
            }

            else
            {
                Response.Write("<script>alert('Username/Password chưa đúng')</script>");
            }
            cnn.Close();
        }
        protected void boqua()
        {
            txtpassword.Text = txtuser.Text = "";
            txtuser.Focus();
        }


        protected void lbtnThoat_Click(object sender, EventArgs e)
        {
            plDangnhap.Visible = true;
            lbtnThoat.Visible = false;
            Session["Name"] = "";
            lblThongbao.Visible = false;
            Response.Redirect(Request.Url + "");
        }

        protected void lbtnDangky_Click(object sender, EventArgs e)
        {
            Response.Redirect("Dangky.aspx");
        }

        protected void lbtnQuenmatkhau_Click(object sender, EventArgs e)
        {

        }

        protected void lbtnQuantri_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin/Admin.aspx");
        }

        protected void lbtnCanhan_Click(object sender, EventArgs e)
        {
            string Name = Session["Name"].ToString();
            int quyen = layMaquyen(Name);
            if (quyen == 3)
            {
                Response.Redirect("Personal/Personal.aspx");
            }
            if (quyen == 2)
            {
                Response.Redirect("PersonalUser/User.aspx");
            }
            
        }

        protected void lbtnThongtinNNC_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThongtinNNC.aspx");
        }

        protected void lbtnThongtinSV_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThongtinSV.aspx");
        }

    }
}